package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func check(e error) {
	if e != nil {
		panic(e)
	}
}
func main() {
	err := os.Mkdir("/tmp/hi", 0755)
	check(err)
	defer os.RemoveAll("/tmp/hi")

	err = os.Mkdir("/tmp/hi/bye", 0755)
	check(err)
	defer os.RemoveAll("/tmp/hi")

	createEmptyFile := func(name string) {
		err := os.WriteFile(name, []byte(""), 0644)
		check(err)
	}

	createEmptyFile("/tmp/hi/file1")
	createEmptyFile("/tmp/hi/bye/file2")
	createEmptyFile("/tmp/hi/bye/file3")

	cdir, err := os.Getwd()
	check(err)
	fmt.Println("current dir: ", cdir)
	c, err := os.ReadDir("/tmp/hi")
	check(err)
	for idx, entry := range c {
		fmt.Println(idx, entry.Name())
	}

	err = os.Chdir("/tmp/hi/bye")
	check(err)
	cdir, err = os.Getwd()
	check(err)
	fmt.Println("current dir: ", cdir)
	c, err = os.ReadDir(".")
	check(err)
	for idx, entry := range c {
		fmt.Println(idx, entry.Name())
	}

	fmt.Println("Visiting subdirs:")
	err = filepath.WalkDir("/tmp/hi", func(path string, d os.DirEntry, err error) error {
		if err != nil {
			return err
		}
		fmt.Println(path, d.IsDir())
		return nil
	})

	check(err)

}
